package com.maysham.tripod.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maysham.tripod.build.docker.BuildImageRequest;
import com.maysham.tripod.dto.DockerImage;
import com.maysham.tripod.service.DockerService;
import com.maysham.tripod.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.security.web.header.Header;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Service
@Slf4j
public class DockerServiceImpl implements DockerService {

    @Resource
    private RestTemplate restTemplate;

    @Override
    public List<DockerImage> listImage(String host) {
        ResponseEntity<String> entity = restTemplate.getForEntity(host + PATH_LIST_IMAGES, String.class);
        if (StringUtils.isEmpty(entity)) {
            return Collections.emptyList();
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(entity.getBody(), new TypeReference<List<DockerImage>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public void buildImage(String host, File tarFile, BuildImageRequest request) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type", "application/tar");
        String reg = "{\n" +
                "  \"docker.example.com\": {\n" +
                "    \"username\": \"janedoe\",\n" +
                "    \"password\": \"hunter2\"\n" +
                "  },\n" +
                "  \"https://index.docker.io/v1/\": {\n" +
                "    \"username\": \"mobydock\",\n" +
                "    \"password\": \"conta1n3rize14\"\n" +
                "  }\n" +
                "}";
        headers.add("X-Registry-Config", Base64.getEncoder().encodeToString(reg.getBytes(StandardCharsets.UTF_8)));
        try {
            RequestEntity<FileSystemResource> requestEntity = new RequestEntity<>(new FileSystemResource(tarFile), headers, HttpMethod.POST, URI.create(host + PATH_BUILD_IMAGE));
            Map<String, String> params = new HashMap<>();
            ResponseEntity<String> result = restTemplate.exchange(host + PATH_BUILD_IMAGE, HttpMethod.POST, requestEntity, String.class, params);
            if (result.getStatusCode() != HttpStatus.OK) {
                log.error(result.getStatusCodeValue() + "");
            }
            log.info(result.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
